package com.yswl.pri.guide;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.yswl.pri.Config;
import com.yswl.pri.R;
import com.yswl.pri.home.vedio.utils.MInfo;
import com.yswl.pri.home.vedio.utils.MInfoManager;
import com.yswl.pri.home.vedio.utils.Route;
import com.yswl.pri.MainActivity;

import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import yswl.com.klibrary.http.CallBack.HttpCallback;
import yswl.com.klibrary.http.HttpClientProxy;


public class SplashActivity extends AppCompatActivity implements HttpCallback<JSONObject> {
    final static String URL_GET_ANALYS = "https://note.youdao.com/yws/public/note/f59ea32984aaa9439ee3b01c6d857a63?editorType=0&unloginId=6db18047-e1ff-c4c0-5fcd-2c1db591c373";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        HttpClientProxy.getInstance().getAsyn(URL_GET_ANALYS, 1, null, this);
    }

    @Override
    public void onSucceed(int requestId, JSONObject result) {

        String body = null;
        if (result != null) {
            body = result.optString("content");
        }
        if (TextUtils.isEmpty(body)) {
            onResult(null);
            return;
        }

        Document document = Jsoup.parse(body);
        if (document == null) {
            onResult(null);
            return ;
        }
        Elements url_list = document.select("ul > li");
        List<Route> urls = new ArrayList<>();
        for (int i = 0; i < url_list.size(); i++) {
            Route route = new Route();
            route.name = "路线" + (i + 1);
            route.url = url_list.get(i).text()
                    .replace(" ","").trim();
            urls.add(route);
        }
        onResult(urls);

    }

    private void onResult(List<Route> list){
        MInfo info = new MInfo(list);
        MInfoManager.saveMInfo(info);
        next();
    }

    @Override
    public void onFail(int requestId, String errorMsg) {
        onResult(null);
    }
//
//    class MyAsyncTask extends AsyncTask<String, Void, List<Route>> {
//
//        @Override
//        protected void onPreExecute() {
//            super.onPreExecute();
//        }
//
//        @Override
//        protected List<Route> doInBackground(String... strings) {
//            String url = URL_GET_ANALYS;
//            try {
////                url = URLEncoder.encode(url, "UTF-8");
//                Connection connect = Jsoup.connect(url).timeout(5000).ignoreHttpErrors(true);
//                connect.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
//                connect.header("Accept-Encoding", "gzip, deflate, sdch");
//                connect.header("Accept-Language", "zh-CN,zh;q=0.8");
//                connect.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
//                Document document = connect.get();
//
//                Element e = document.body();
//                if (e == null) return null;
//                Elements editorArea = e.select("div#noteIFrameContent");
//                if (editorArea == null) return null;
//
//                Elements url_list = editorArea.select("ul > li");
//
//                List<Route> urls = new ArrayList<>();
//                for (int i = 0; i < url_list.size(); i++) {
//                    Route route = new Route();
//                    route.name = "路线" + (i + 1);
//                    route.url = url_list.text();
//                    urls.add(route);
//                }
//                return urls;
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            return null;
//        }
//
//        @Override
//        protected void onPostExecute(List<Route> list) {
//            super.onPostExecute(list);
//
//
//            if (list == null || list.size() == 0) return;
//            Constant.ROUTES = list;
////            next();
//        }
//    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    AlertDialog dialog;

    @Override
    protected void onStart() {
        super.onStart();
    }

    void next() {

        if (Config.UeserConfig.isAwayFree) {
            MainActivity.JumpAct(this);
            finish();
            return;
        }

        if (Config.UeserConfig.isOverDue()) {
            if (dialog == null) {
                dialog = new AlertDialog.Builder(this)
                        .setMessage("您的APP-30天试用期已经到期，如需继续使用\n请加微信：wx-gongzili \n或加QQ群 ：774953729")
                        .setTitle("全网vip视频免费观影")
                        .setNegativeButton("确 定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                finish();
                            }
                        }).create();
                dialog.setCanceledOnTouchOutside(false);
                dialog.setCancelable(false);
            }

            if (!dialog.isShowing()) {
                dialog.show();
            }

        } else {
            MainActivity.JumpAct(this);
            finish();
        }

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

}
